package com.guangzhi.weijianzhi.activity;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.guangzhi.weijianzhi.R;
import com.guangzhi.weijianzhi.http.HttpRequestUtils;
import com.guangzhi.weijianzhi.http.MyAsyncHttpResponseHandler;
import com.guangzhi.weijianzhi.view.LodingFragmentDialog;

public class RegisterFinishActivity extends BaseActivity implements OnClickListener {

	private EditText passwordEdt,okPasswordEdt;
	private CheckBox cb;
	private TextView mTv_text;
	private String phone,code;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		phone = getIntent().getStringExtra("phone");
		code = getIntent().getStringExtra("code");
		setContentView(R.layout.tz_register_finish_layout);
		
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		initTitle();
		titleTextV.setText("密码");
		passwordEdt = (EditText)findViewById(R.id.password_edt);
		okPasswordEdt = (EditText)findViewById(R.id.ok_password_edt);
		cb = (CheckBox) findViewById(R.id.checkBox);
		mTv_text = (TextView) findViewById(R.id.tv_text);
		Button mFinishBut = (Button) findViewById(R.id.finish_btn);
		goBack.setOnClickListener(this);
		mTv_text.setText(Html.fromHtml("<u>" + "用户协议" + "</u>"));
		mTv_text.setOnClickListener(this);
		mFinishBut.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_text:
			Intent i = new Intent(RegisterFinishActivity.this, XieYiActivity.class);
			startActivity(i);
			break;

		case R.id.title_back:
			onBackPressed();
			break;
			
		case R.id.finish_btn:
			 if (passwordEdt.getText().toString().equals("")) {
				toastUtil("请输入注册密码");
			} else if (okPasswordEdt.getText().toString().equals("")) {
				toastUtil("请输入确认密码");
			} else if (!passwordEdt.getText().toString()
					.equals(okPasswordEdt.getText().toString())) {
				toastUtil("两次输入密码不一致,请重新输入");
			}  else if (!cb.isChecked()) {
				toastUtil("您需要同意用户协议");
			} else{
				doRegister(phone, passwordEdt.getText().toString(), code);
			}
			
			
			break;
			
		}
	}
	public void toastUtil(String str) {
		Toast.makeText(RegisterFinishActivity.this, str, Toast.LENGTH_SHORT).show();
	}
    private void doRegister(String tel, String password, String messageCode) {
		// TODO Auto-generated method stub
   	 HttpRequestUtils.doHttpRegister(tel, password, messageCode, new MyAsyncHttpResponseHandler(getSupportFragmentManager(),getResources()) {
   		 
   		 @Override
   		public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
   			// TODO Auto-generated method stub
   			super.onSuccess(arg0, arg1, arg2);
   			
   			try {
					JSONObject jsonO = new JSONObject(new String(arg2));
					
					if(jsonO.optBoolean("status")){
						JSONObject optJSONObject = jsonO.optJSONObject("token");
						String token = optJSONObject.optString("access_token");
						
						Intent intent = new Intent(RegisterFinishActivity.this,UserAndCompanyActivity.class);
						intent.putExtra("token", token);
						startActivity(intent);
						
					}else{
						toastUtil(jsonO.optString("errMessage"));
					}
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
   			
   		}
   		 
   		 @Override
   		public void onFinish() {
   			// TODO Auto-generated method stub
   			super.onFinish();
   			LodingFragmentDialog.dismiss(getSupportFragmentManager());
   		}
   		 
		});
	}
}
